package web.manager;

import bean.Manager;
import com.alibaba.fastjson.JSON;
import entity.ManagerEntity;
import util.FANREN;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "SignUp", urlPatterns = "/dao/update/signUp")
public class SignUp extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String jsonString = FANREN.getJson(request);
        System.out.println(jsonString);
        EntityManager em = (EntityManager) request.getAttribute("_entity.Manager");

        Manager managerNew = JSON.parseObject(jsonString, Manager.class);
        Manager managerOld = (Manager) request.getSession().getAttribute("manager");

        ManagerEntity managerEntity= new ManagerEntity();
        managerEntity.setName(managerOld.getName());
        managerEntity.setPwd(managerNew.getPwd());
        managerEntity.setUsername(managerOld.getUsername());
        em.persist(managerEntity);
        Manager manager = new Manager(managerEntity.getMid(),managerEntity.getName(),managerEntity.getPwd(),managerEntity.getUsername());
        request.getSession().setAttribute("manager",manager);
    }
}

